package com.qf.cloud2204.coupons.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.cloud2204.coupons.entity.Coupon;
import com.qf.cloud2204.coupons.mapper.CouponMapper;
import com.qf.cloud2204.coupons.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxz
 * @since 2023-05-19
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String createCoupons(BigDecimal amount, Integer count) {

        if (amount!=null&&count!=null){
            //生成批次号
            String batchId = UUID.randomUUID().toString();
            ArrayList<Coupon> couponsList = new ArrayList<>();
            for (int i = 0; i < count; i++) {
                //生成优惠券id
                String couponId = UUID.randomUUID().toString();
                Coupon coupon = new Coupon();
                coupon.setBatchId(batchId);
                coupon.setId(couponId);
                coupon.setAmount(amount);
                coupon.setScope("食品类");
                coupon.setCreatedTime(LocalDateTime.now());
                //自从发放优惠券日起30日有效期
                coupon.setExpireTime(LocalDateTime.now().plusDays(30));
                coupon.setIsReceived("0");
                //存入到集合中
                couponsList.add(coupon);
            }
            this.saveBatch(couponsList);
            return batchId;
        }else {
            return null;
        }

    }


    @Override
    public boolean putCoupons(String batchId) {
        //根据批次号查询一批优惠券
        List<Coupon> coupons = couponMapper.selectList(new QueryWrapper<Coupon>().eq("batch_id", batchId));
        String key = "coupons:put:"+batchId;
        //把优惠券的id提取到一个list中
        List<String> Idlist = coupons.stream().map(Coupon::getId).collect(Collectors.toList());
        for (String s : Idlist) {
            redisTemplate.opsForSet().add(key,s);
        }

        return true;
    }

    //抢券
    @Override
    public boolean grab(String batchId,String number) {
        //判断一下此时set里是否还有优惠券
        Long size = redisTemplate.opsForSet().size("coupons:put:" + batchId);

        if (size!=null){//如果还有
            //从set里弹出一个优惠券id
            String id = (String) redisTemplate.opsForSet().pop("coupons:put:" + batchId);
            String key = "coupons:grab:"+number;
            redisTemplate.opsForList().rightPush(key,id);
            String exchangeName="cupon-exchage";//默然交换机是空串
            String msg = "用户"+number+"抢券成功";
            rabbitTemplate.convertAndSend(exchangeName,"",msg);
            return  true;
        }else {
            return false;
        }

    }
}
